package com.fruitshop.util;

import com.fruitshop.model.Admin;
import com.fruitshop.model.User;
import com.fruitshop.model.UserInfo;

public class UserInfoContext {
    static ThreadLocal<UserInfo> userInfoThreadLocal = new ThreadLocal<>();

    public static void setUserInfo(UserInfo userInfo) {
        userInfoThreadLocal.set(userInfo);
    }

    public static UserInfo getUserInfo() {
        return userInfoThreadLocal.get();
    }

    public static void remove() {
        userInfoThreadLocal.remove();
    }

    public static boolean isLogin() {
        return getUserInfo() != null;
    }

    public static boolean isAdmin() {
        UserInfo userInfo = getUserInfo();
        return userInfo!=null&&userInfo.isAdmin();
    }

    public static User getUser() {
        UserInfo userInfo = getUserInfo();
        return userInfo!=null?userInfo.getUser():null;
    }

    public static Admin getAdmin() {
        UserInfo userInfo = getUserInfo();
        return userInfo!=null?userInfo.getAdmin():null;
    }
}
